<?php

/**
 * @filename Chat.class.php 
 * @encoding UTF-8 
 * @author  fwz 
 * @datetime 2018-7-28  11:10:10
*/

class Chat {
    
    public static function open($data) {
        $pushMsg = array();
        $pushMsg['code'] = 4;
        $pushMsg['msg'] = 'success';
        $pushMsg['data']['mine'] = 0;
        $pushMsg['data']['rooms'] = self::getRooms();   //房间列表
        $pushMsg['data']['users'] = self::getOnlineUsers();        
        unset($data);
        return $pushMsg;
    }
    
    //返回房间列表
    public static  function getRooms(){
        global $rooms;
        $roomss = array();
        foreach($rooms as $k=>$v){
            $roomss[] = array(
                'roomid'=>$k,
                'roomname'=>$v
            );
        }
        return $roomss;
    }
    
    //在线用户列表
    public static function getOnlineUsers(){
        $user =new ChatUser();
        $lists = $user->getOnlineUsers();
        
        $users = array();
        foreach($lists as $key=>$val){
            $users[$key] = $user->getUsers($key,  array_slice($val, 0,100));
        }
        return $users;
    }
    
    public static function logout($roomid,$fd){
        $user = new ChatUser();
        $userinfo = $user->getUser($roomid,$fd);
        return $userinfo;
    }

    //登陆
    public static function dologin($data){
        $pushmsg = array();
        $pushmsg['code'] = 1;
        $pushmsg['msg'] = $data['params']['name']."加入了群聊";
        $pushmsg['data']['roomid'] = $data['roomid'];
        $pushmsg['data']['fd'] = $data['fd'];
        $pushmsg['data']['name'] = $data['params']['name'];
        $pushmsg['data']['avatar'] = DOMAIN.'/static/images/avatar/f1/f_'.rand(1,12).'.jpg';
	$pushmsg['data']['time'] = date("H:i",time());
        
        self::login($data['roomid'],$data['fd'],$data['params']['name'],$data['params']['email'],$pushmsg['data']['avatar']);
        
        unset($data);
        return $pushmsg;
    }
    
    //登陆
    public static function login($roomid,$fd,$name,$email,$avatar){
        if($name == ""){
            $name = "游客".time();
        }
        if($email == ''){
            $email = "xxx@qq.com";
        }
        echo $fd."==8989===";
        
        $user = new ChatUser(array(
            'name'=>$name,
            'email'=>$email,
            'roomid'=>$roomid,
            'fd'=>$fd,
            'avatar'=>$avatar
        ));
        $user->save();
    }
    
    //发送新消息
    public static function sendNewMsg($data){
        $pushmsg = array();
        $pushmsg['code'] = 2;
        $pushmsg['msg'] = '';
        $pushmsg['data']['roomid'] = $data['roomid'];
        $pushmsg['data']["fd"] = $data['fd'];
        $pushmsg['data']['name'] = $data['params']['name'];
        $pushmsg['data']['email'] = $data['params']['email'];
        $pushmsg['data']['newmessage'] = escape(htmlspecialchars($data['message']));
        $pushmsg['data']['remains'] = array();
        if($data['c'] == 'img'){
            $pushmsg['data']['newmessage'] = '<img class="chat-img" onclick="preview(this)" style="display: block; max-width: 120px; max-height: 120px; visibility: visible;" src='.$pushmsg['data']['newmessage'].'>';
        }else{
            global $emotion;
            foreach ($emotion as $key => $value) {
                $pushmsg['data']['newmessage'] = str_replace($key, $value, $pushmsg['data']['newmessage']);
            }
            $tmp = self::remind( $pushmsg['data']['roomid'],  $pushmsg['data']['newmessage']);
            if($tmp){
                $pushmsg['data']['newmessage'] =  $tmp['msg'];
                $pushmsg['data']['remains']    = $tmp['remains'];
            }
            unset( $tmp);
        }
        $pushmsg['data']['time'] = time();
        unset($data);
        return $pushmsg;
    }
    
    // 正则匹配出所有@的人
    public static function remind($roomid,$msg){
        $data = array();
        if($msg != ''){
            $data['msg'] = $msg;
            $s = preg_match_all('~@(.+?) ~', $msg, $matches);
            if($s){
                $m1 = array_unique($matches[0]);
                $m2 = array_unique($matches[1]);
                $user = new ChatUser();
                $users = $user->getUsersByRoom($roomid);
                $m3 = array();
                foreach ($users as $key => $value) {
                    $m3[$value['name']] = $value['fd'];
                }
                $i = 0;
                foreach ($m2 as $key => $value) {
                    if(array_key_exists($value,$m3)){
                        
                        $data['msg'] = str_replace($m1[$key], '<font color="blue">'.trim($m1[$key]).'</font>', $data['msg']);
                        $data['remains'][$key]['fd'] = $m3[$value]; 
                        $data['remains'][$key]['name'] = $value;
                        $i++;
                    }
                }
                unset($users);
                unset($m1,$m2,$m3);
            }
            unset($s);
        }
        return $data;
    }
    
    //退出登陆
    public static function doLogout($data){
        File::Logout($data['fd']);
        $pushmsg = array();
        $pushmsg['code'] = 3;
        $pushmsg['msg'] = $data['params']['name'].'退出了群聊';
        $pushmsg['data']['name'] = $data['params']['name'];
        $pushmsg['data']['fd'] = $data['fd'];        
        
        unset($data);
        return $pushmsg;
    }
    
    public static function noLogin($data){
        $pushmsg = array();
        $pushmsg['code'] = 5;
        $pushmsg['msg'] = "系统不会存储您的Email，只是为了证明你是一个地球人";
        if($data['params']['name']){
             $pushmsg['msg'] = "输入一个昵称让更多的人了解下你";
        }
        $pushmsg['data']['mine'] = 1;
        
        unset($data);
        return $pushmsg;
    }
    
    //换房间
    public static function change($data){
        $pushmsg = array();
        $pushmsg['code'] = 6;
        $pushmsg['msg'] = "换房成功";
        $user = new ChatUser();
        $is_copyed = $user->changeUser($data['oldroomid'],$data['fd'],$data['roomid']);
        if($is_copyed){}
        $pushmsg['data']['oldroomid'] = $data['oldroomid'];
        $pushmsg['data']['roomid'] = $data['roomid'];
        
        $pushmsg['data']['mine'] = 0;
        $pushmsg['data']['name'] = $data['params']['name'];
        $pushmsg['data']['email'] = $data['params']['email'];
        $pushmsg['data']['fd'] = $data['fd'];
        $pushmsg['data']['avatar'] = $data['params']['avatar'];
        $pushmsg['data']['time'] = date("H:i",time());
        
        unset($data);
        return $pushmsg;
    }
    
    
    
}